package me.chyxion.blog.controller;

import java.util.List;
import me.chyxion.blog.model.User;
import me.chyxion.blog.form.UserForm;
import me.chyxion.blog.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Apr 19, 2018 17:04:48
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

    @RequestMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    @RequestMapping("/find")
    public User find(@RequestParam("id") int id) {
        return userService.find(id);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public void create(UserForm form) {
        final User user = new User();
        BeanUtils.copyProperties(form, user);
        userService.create(user);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(UserForm form) {
        final User user = new User();
        BeanUtils.copyProperties(form, user);
        userService.update(user);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void delete(@RequestParam("id") int id) {
        userService.find(id);
    }
}